﻿using System;
using Gj;
using UnityEngine;
using UnityEngine.UI;

public class MiniMap : MonoBehaviour
{
    public GameObject Body;
    public Camera Camera;
    public Vector2 offset;
    public bool full;
    public float ratio = 0;
    public Image image;
    public float cameraRatio = 1;

    public float minScale = 1;
    public float maxScale = 2;
    public float scale = 1;

    public Vector2 bodySize;
    public Vector2 size;

    public Vector3 position = Vector3.zero;

    public Vector2 offsetAnchored = Vector2.zero;
    public UnityObject target;

    public MiniMapItem[] itemList;

    public Vector3 CameraPosition
    {
        get => Camera.transform.position;
        set
        {
            if (target == null)
                position = value;
            else
                position = target.GetPosition();

            Camera.transform.position = position;
        }
    }

    public void OnInit()
    {
        offsetAnchored = image.rectTransform.anchoredPosition;
    }

    public void OnInit(float x, float y)
    {
        float height = (x > y ? x : y) * cameraRatio;
        Camera.transform.localPosition = new Vector3(0, height, 0);
        float angle = Camera.fieldOfView / 2;
        float length = size.y / 2;
        ratio = length / ((float)Math.Tan(angle * Math.PI / 180) * height);
    }

    public void SetFull(bool b)
    {
        if (b == full) return;
        full = b;
    }

    public void Show()
    {
        Camera.gameObject.SetActive(true);
        gameObject.SetActive(true);
    }

    public void Hide()
    {
        Camera.gameObject.SetActive(false);
        gameObject.SetActive(false);
    }

    public void SetTarget(UnityObject t)
    {
        target = t;
    }
}